/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DLSched;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalINIConfiguration;
/**
 *
 * @author HP
 */
public class DLHtml
{
    public static boolean download_file(String url_query) throws ConfigurationException
    {
        /* Initializing the config_file */
        final HierarchicalINIConfiguration config =
                new HierarchicalINIConfiguration("config.ini");
        
        /* Initializing the logger */
        Logger DLHtml_logger = Logger.getLogger("DLHtml_log");
        DLHtml_logger.log(Level.INFO, "Initialized DLHtml");
        
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try
        {
            URL url = new URL(url_query);
            URLConnection url_con = url.openConnection();
            if (!config.getString("proxy.host").isEmpty())
            {
                DLHtml_logger.log(Level.INFO, "Using Proxy -> " 
                        + config.getString("proxy.host") + ":" 
                        + config.getString("proxy.port"));
                System.setProperty("http.proxyHost",
                                   config.getString("proxy.host"));
                System.setProperty("http.proxyPort",
                                   config.getString("proxy.port"));
                Authenticator.setDefault(new Authenticator()
                {
                    protected PasswordAuthentication getPasswordAuthentication()
                    {
                        return new PasswordAuthentication(
                            config.getString("proxy.username"),
                            config.getString("proxy.password").toCharArray());
                    }
                });
            }
            else
                DLHtml_logger.log(Level.INFO, "Not using proxy");
            
            bis = new BufferedInputStream(url_con.getInputStream());
            bos = new BufferedOutputStream(
                    new FileOutputStream(config.getString("download.filepath")));
            int i;
            while ((i = bis.read()) != -1)
            {
                bos.write(i);
            }
            DLHtml_logger.log(Level.INFO, "Successfully downloaded " 
                    + config.getString("download.filepath"));
            return true;
        }
        catch (Exception e)
        {
            DLHtml_logger.log(Level.SEVERE, "Exception in download");
            e.printStackTrace();
            return false;
        }
        finally
        {
            try
            {
                bis.close();
                bos.close();
            }
            catch (IOException e)
            {
                DLHtml_logger.log(Level.SEVERE, "Exception closing iostream");
                e.printStackTrace();
            }
        }
    }
    public static void main(String [] args) throws Exception
    {
        final HierarchicalINIConfiguration config =
                new HierarchicalINIConfiguration("config.ini");
        System.out.println("Testing download_file");
        DLHtml.download_file("http://www.filestube.com/search.html?q=top+gear+s17e03&select=All");
        ParseHtml.get_links(config.getString("download.filepath"));
    }
}
